from agno.models.openai import OpenAILike

from configs.basic_config import ALI_GENERATION_MODEL, AGENT_URL, ALI_DASHSCOPE_API_KEY
from agno.agent import Agent
#from agno.db.postgres import PostgresDb

from agno.os import AgentOS
from agno.tools.baidusearch import BaiduSearchTools
# from agno.tools.duckduckgo import DuckDuckGoTools
from fastapi import FastAPI

# 设置数据库
#db = PostgresDb(db_url="postgresql+psycopg://ai:ai@localhost:5532/ai")

web_research_agent = Agent(
    id="web-research-agent",
    name="网络研究代理",
    # --- 修改模型参数 ---
    model=OpenAILike(id=ALI_GENERATION_MODEL, name=ALI_GENERATION_MODEL, api_key=ALI_DASHSCOPE_API_KEY, base_url=AGENT_URL),
    #db=db,
    tools=[BaiduSearchTools()],
    add_history_to_context=True,
    num_history_runs=3,
    add_datetime_to_context=True,
    markdown=True,
)

# 自定义 FastAPI 应用
app: FastAPI = FastAPI(
    title="自定义 FastAPI 应用",
    version="1.0.0",
)


# 添加您自己的路由
@app.post("/customers")
async def get_customers():
    return [
        {
            "id": 1,
            "name": "John Doe",
            "email": "john.doe@example.com",
        },
        {
            "id": 2,
            "name": "Jane Doe",
            "email": "jane.doe@example.com",
        },
    ]


# 设置我们的 AgentOS 应用，传入您的 FastAPI 应用
# 使用 route_prefix 避免与您自定义的路由发生冲突
agent_os = AgentOS(
    description="带有自定义路由的示例应用",
    agents=[web_research_agent],
    base_app=app,
)

# 或者，将 AgentOS 应用的所有路由添加到当前应用中
# for route in agent_os.get_routes():
#     app.router.routes.append(route)

app = agent_os.get_app()


if __name__ == "__main__":
    """运行您的 AgentOS。

    使用此设置：
    - API 文档: http://localhost:7777/docs

    """
    # need uvicorn
    agent_os.serve(app="custom_fastapi:app", reload=True)